import gqlRequest from "@/utils/gqlRequest";
import { gql } from "apollo-boost";
import { toInteger } from "lodash";

const GET_AUTHOR = gql`
  query Author($id: Int!) {
    author(id: $id) {
      id
      firstName
      lastName
      posts {
        id
        title
        votes
      }
    }
  }
`;

const VOTE_POST = gql`
  mutation VotePost($data: UpvotePostInput!) {
    upvotePost(upvotePostData: $data) {
      id
      title
      votes
    }
  }
`;

export async function queryPosts(id: string): Promise<any> {
  return gqlRequest.query({
    query: GET_AUTHOR,
    variables: { id: toInteger(id) },
  });
}

export async function votePost(id: string): Promise<any> {
  return gqlRequest.mutate({
    mutation: VOTE_POST,
    variables: {
      data: {
        id: toInteger(id),
        votes: 1,
      },
    },
  });
}
